<?php
namespace app\scodetrace\controller;

use \app\scodetrace\encoder\TcodeEncoder;

// 实现扫码写入log，后期绑定防伪码与批次
// 实现通过扫码对数据进行批量修改
// 接口按功能分为两类，一类是提交数据接口，一类是提交批次的接口
//
class Scanner extends \app\common\controller\ScannerBase {
  /**
   * to write a log to a scode in its trace record.
   * @return [type] [description]
   */
  public function startTraceLog() {
    return $this->_dataStore(['logtext','area_id','result'],'do_writeTraceLog');
  }

  protected function do_writeTraceLog($data, $codes) {
    if ($codes['type']!='tcode') {
      return json_pack("溯源码错误或未指定");
    }
    $ids = $codes['ids'];
    if (!($logtext=$data['logtext'])) {
      return json_pack('log内容不能为空',400);
    }
    if (!($area_id=$data['area_id'])) {
      return json_pack('操作地点不能为空',400);
    }
    if (!($result=$data['result'])) {
      return json_pack('操作结果不能为空',400);
    }

    $scodeModel=model('Scode');
    $err=$scodeModel->logOneTrace($ids,$logtext,$area_id,$result);
    if (!count($err))
      return json_pack('OK');
    else {
      $encoder = TcodeEncoder::getInstance();
      foreach($err as &$v) {
       $v['tcode'] = $encoder->encode($v['ids']);
       unset($v['ids']);
      }
      return json_pack($err,400);
    }
  }
}
?>